import ObjectUtil from './object-util'
import dayjs from 'dayjs'

export default class TimeUtil {
  constructor(interval) {
    this.intervalSeconds = interval
  }
    intervalSeconds=0;
    intervalHandler=null;
    startIntervalProcess(func) {
      const that = this
      if (ObjectUtil.isNull(func)) {
        return
      }
      this.stopIntervalProcess()
      console.log('out', that.intervalSeconds)
      this.intervalHandler = setInterval(function() {
        func(that.intervalSeconds)
      }, this.intervalSeconds * 1000)
    }
    stopIntervalProcess() {
      console.log('stopIntervalProcess', this.intervalSeconds)
      if (ObjectUtil.notNull(this.intervalHandler)) {
        clearInterval(this.intervalHandler)
        this.intervalHandler = null
      }
    }

    static getTimeZone = function() {
      return 8
    }
    static getTimeFormatStr = function() {
      return 'HH:mm:ss'
    }
    static getDateFormatStr = function() {
      return 'YYYY-MM-DD'
    }
    static getDateTimeFormatStr = function() {
      return TimeUtil.getDateFormatStr() + ' ' + TimeUtil.getTimeFormatStr()
    }

    static formatNumber = function(n) {
      try {
        n = n.toString()
        return n[1] ? n : '0' + n
      } catch (e) {
        console.log(e)
        return 0
      }
    }
    static getDateDiffDays = function(date_start, date_end) {
      let dateSpan = date_end - date_start
      dateSpan = Math.abs(dateSpan)
      const iDays = Math.floor(dateSpan / (24 * 3600 * 1000))
      return iDays
    }
    static getShortDate = function(date) {
      let date_str
      // eslint-disable-next-line valid-typeof
      if (typeof date === 'Date') {
        date_str = TimeUtil.formatDate(date)
      } else {
        date_str = '' + date
      }
      const now_str = TimeUtil.formatDate(new Date())
      const len = date_str.length
      for (let i = 0; i < len; i++) {
        if (now_str.charAt(i) !== date_str.charAt(i)) {
          return date_str.substr(i, len - i)
        }
      }
      return date_str
    }
    static getShortTime = function(date) {
      let date_str
      // eslint-disable-next-line valid-typeof
      if (typeof date === 'Date') {
        date_str = TimeUtil.formatDateTime(date)
      } else {
        date_str = '' + date
      }
      const now_str = TimeUtil.formatDateTime(new Date())
      const len = date_str.length
      for (let i = 0; i < len; i++) {
        if (now_str.charAt(i) !== date_str.charAt(i)) {
          return date_str.substr(i, len - i)
        }
      }
      return date_str
    }

    static formatDate = function(date) {
      return TimeUtil.formatDateWithFmt(date, TimeUtil.getDateFormatStr())
    }
    static formatDateTime = function(date) {
      return TimeUtil.formatDateWithFmt(date, TimeUtil.getDateTimeFormatStr())
    }
    static formatDateWithFmt = function(date, fmt) {
      // if (/(y+)/.test(fmt)) {
      //     fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
      // }
      // const o = {
      //     'M+': date.getMonth() + 1,
      //     'd+': date.getDate(),
      //     'h+': date.getHours(),
      //     'm+': date.getMinutes(),
      //     's+': date.getSeconds()
      // }
      // for (const k in o) {
      //     if (new RegExp(`(${k})`).test(fmt)) {
      //         const str = o[k] + ''
      //         fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substr(str.length))
      //     }
      // }
      // return fmt
      const obj = dayjs(date)
      if (obj.isValid()) {
        return obj.format(fmt)
      }
      return null
    }

    static formatTimeStamp= function(timeStamp) {
      // var date = new Date()
      // date.setTime(timeStamp)
      // var y = date.getFullYear()
      // var m = date.getMonth() + 1
      // m = m < 10 ? ('0' + m) : m
      // var d = date.getDate()
      // d = d < 10 ? ('0' + d) : d
      // var h = date.getHours()
      // h = h < 10 ? ('0' + h) : h
      // var minute = date.getMinutes()
      // var second = date.getSeconds()
      // minute = minute < 10 ? ('0' + minute) : minute
      // second = second < 10 ? ('0' + second) : second
      // return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
      const obj = dayjs(timeStamp)
      if (obj.isValid()) {
        return obj.format(TimeUtil.getDateTimeFormatStr())
      }
      return null
    }
    static formatTimeStampToDate= function(timeStamp) {
      // var date = new Date()
      // date.setTime(timeStamp)
      // var y = date.getFullYear()
      // var m = date.getMonth() + 1
      // m = m < 10 ? ('0' + m) : m
      // var d = date.getDate()
      // d = d < 10 ? ('0' + d) : d
      //
      // return y + '-' + m + '-' + d
      const obj = dayjs(timeStamp)
      if (obj.isValid()) {
        return obj.format(TimeUtil.getDateFormatStr())
      }
      return null
    }

    static getFirstDayOfMonth = function(date) {
      const obj = dayjs(date)
      if (obj.isValid()) {
        return obj.startOf('month').toDate()
      }
      return null
    }

    /**
     * 获取当前月的最后一天
     */
    static getLastDayOfMonth = function(date) {
      // var d = new Date(date);
      // var nextMonth = TimeUtil.addMonths(d, 1);
      // var lastDay = TimeUtil.addDays(nextMonth, -1)
      // return lastDay
      const obj = dayjs(date)
      if (obj.isValid()) {
        return obj.endOf('month').toDate()
      }
      return null
    }

    static addDays = function(date, days) {
      // let d = new Date(date)
      // d.setTime(d.setDay(d.getDay() + days));
      // return d
      const obj = dayjs(date)
      if (obj.isValid()) {
        return obj.add(days, 'day').toDate()
      }
      return null
    }

    static addMonths = function(date, months) {
      // let d = new Date(date)
      // d.setTime(d.setMonth(d.getMonth() + months));
      // return d
      const obj = dayjs(date)
      if (obj.isValid()) {
        return obj.add(months, 'month').toDate()
      }
      return null
    }

    static formatDate = function(date, date_splitter) {
      const obj = dayjs(date)
      if (obj.isValid()) {
        date_splitter = date_splitter || '-'
        // var year = date.getFullYear()
        // var month = date.getMonth() + 1
        // var day = date.getDate()
        //
        // return [year, month, day].map(TimeUtil.formatNumber).join(date_splitter)
        const formatStr = `YYYY${date_splitter}MM${date_splitter}DD`

        return obj.format(formatStr)
      }
      return null
    }

    static formatTime = function(date, date_splitter, time_splitter) {
      const obj = dayjs(date)
      if (obj.isValid()) {
        date_splitter = date_splitter || '-'
        time_splitter = time_splitter || ':'

        const formatStr = `YYYY${date_splitter}MM${date_splitter}DD HH${time_splitter}mm${time_splitter}ss`
        return obj.format(formatStr)
      }
      return null
    }

    // 秒数转换成00:00:00格式
    static secondsToTime = function(seconds) {
      // let _time = '';
      // let _s = Math.floor(seconds%60)
      // let _m = Math.floor(seconds/60)
      // let _mm = Math.floor(seconds%3600/60)
      // let _h = Math.floor(seconds/3600)
      // if(seconds<=0){
      //     _time = '00:00:00'
      // }else if(seconds < 60){
      //     _time = '00:00:'+(_s<10?`0${_s}`:_s)
      // }else if(seconds < 3600){
      //     _time = '00:'+(_m<10?`0${_m}`:_m)+':'+(_s<10?`0${_s}`:_s)
      // }else{
      //     _time = (_h<10?`0${_h}`:_h)+':'+(_mm<10?`0${_mm}`:_mm)+':'+(_s<10?`0${_s}`:_s)
      // }
      // return _time;

      const obj = dayjs(seconds * 1000).add(-1 * TimeUtil.getTimeZone(), 'hour')
      if (obj.isValid()) {
        return obj.format(TimeUtil.getTimeFormatStr())
      }
      return null
    }
    // 00:00:00格式转换成秒数
    static TimeToseconds = function(time) {
      // var time = '00:02:10';
      time = time.trim(time)
      var hour = time.split(':')[0].trim()
      var min = time.split(':')[1].trim()
      var sec = time.split(':')[2].trim()

      let s = Number(hour * 3600) + Number(min * 60) + Number(sec)
      // console.log(s);//130
      return s
    }
    // 毫秒转秒
    static millisecondtoSecond = function(time) {
      return (Number(time)/1000)
    }
}
